package com.bagua.sbbcshop.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * swagger 配置类
 * @author xhou
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, // url
                "list", // docExpansion => none | list
                "alpha", // apiSorter => alpha
                "schema", // defaultModelRendering => schema
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, // enableJsonEditor => true | false
                true, null);
    }
    /**
     * 定义api组，
     */
    @Bean
    public Docket sbbcAdminApi() {
        List<Parameter> parameterList = Lists.newArrayList();
        ParameterBuilder Authorization = new ParameterBuilder();
        Authorization.name("S-Authorization").description("认证token").
                modelRef(new ModelRef("String")).parameterType("header").required(true).defaultValue("SBbc 06764f6f3f9098c31979ab6e6a837267");
        parameterList.add(Authorization.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("sbbcAdminApi")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bagua.sbbcshop.admincontroller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(sbbcAdminApiInfo());
    }

    private ApiInfo sbbcAdminApiInfo() {
        return new ApiInfoBuilder()
                .title("sbbcAdminApi")//大标题
                .description("s2B2b2c 二级分销商城系统 后台接口")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("sbbc")
                .contact(new Contact("xhou", "http://blog.sina.com.cn/wang543202", "925147265@qq.com"))//作者
                .build();
    }
    /**
     * 定义api组，
     */
    @Bean
    public Docket sbbcApi() {
//        List<Parameter> parameterList = Lists.newArrayList();
//        ParameterBuilder Authorization = new ParameterBuilder();
//        Authorization.name("S-Authorization").description("认证token").
//                modelRef(new ModelRef("String")).parameterType("header").required(true).defaultValue("SBbc 06764f6f3f9098c31979ab6e6a837267");
//        parameterList.add(Authorization.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("sbbcApi")
                .enable(true)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
//                .globalOperationParameters(parameterList)
                .forCodeGeneration(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bagua.sbbcshop.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(boolean.class, Boolean.class)
                .apiInfo(sbbcApiInfo());
    }

    private ApiInfo sbbcApiInfo() {
        return new ApiInfoBuilder()
                .title("sbbcApi")//大标题
                .description("s2B2b2c 二级分销商城系统")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("sbbc")
                .contact(new Contact("xhou", "http://blog.sina.com.cn/wang543202", "925147265@qq.com"))//作者
                .build();
    }
}
